package no.ntnu.fp.model;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import no.ntnu.fp.gui.EventDispatcher;
import no.ntnu.fp.model.events.DialogChangeEvent;
import no.ntnu.fp.model.events.DialogEmployeesChangeEvent;
import no.ntnu.fp.model.events.DialogEmployeesClearedEvent;
import no.ntnu.fp.model.events.DialogRoomScheduleChangeEvent;
import no.ntnu.fp.model.events.DialogRoomsClearedEvent;
import no.ntnu.fp.utils.MeetingRoomSchedule;

public class DialogModel {
	private Meeting meeting;
	private Appointment appointment;
	private StateOfDialog state;
	private List<MeetingRoom> rooms;
	private List<Employee> participants;
	private Notice notice;

	public enum StateOfDialog{
		APPOINTMENT, INVITED, OWN_MEETING, MEETING;
	}

	private static DialogModel instance;
	public static void createInstance(){
		if(instance != null){
			throw new IllegalStateException("DialogModel allready exist!");
		}else{
			instance = new DialogModel();
		}
	}
	public static DialogModel getInstance(){
		if(instance == null){
			throw new IllegalStateException("DialogModel instance does not exist!");
		}else{
			return instance;
		}
	}
	
	private DialogModel(){
		state = StateOfDialog.APPOINTMENT;
		meeting = null;
		appointment = null;
		rooms = new ArrayList<MeetingRoom>();
		participants = new ArrayList<Employee>();
	}
	public void setAppointment(StateOfDialog newState, Appointment newAppointment){
		DialogChangeEvent evt = null;
		synchronized (this) {
			evt = new DialogChangeEvent(this, this.appointment, newAppointment, 
					this.state, this.notice, newState);
			this.appointment = newAppointment;
			this.state = newState;
		}
		EventDispatcher.dispatchEvent(evt);
	}
	public void setMeeting(StateOfDialog newState, Meeting newMeeting){
		DialogChangeEvent evt = null;
		synchronized(this) {
			evt = new DialogChangeEvent(this, this.meeting, newMeeting, 
					this.state, this.notice, newState);
			this.meeting = newMeeting;
			this.state = newState;
		}
		EventDispatcher.dispatchEvent(evt);
	}
	public void setRoomSchedule(MeetingRoomSchedule newRoomSchedule){
		DialogRoomScheduleChangeEvent evt = null;
		List<MeetingRoom> newRooms = newRoomSchedule.getAllMeetingRooms();
		synchronized (this) {
			evt = new DialogRoomScheduleChangeEvent(this, this.rooms, 
													newRooms, newRoomSchedule);
			this.rooms.clear();
			this.rooms.addAll(newRooms);
		}
		EventDispatcher.dispatchEvent(evt);
	}
	
	public void setEmployees(List<Employee> newEmployees){
		DialogEmployeesChangeEvent evt = null;
		synchronized (this) {
			evt = new DialogEmployeesChangeEvent(this, 
					this.participants, newEmployees);
			this.participants.clear();
			this.participants.addAll(newEmployees);
		}
		EventDispatcher.dispatchEvent(evt);
	}
	public void clearModel() {
		synchronized(this) {
			this.rooms.clear();
			this.participants.clear();
		}
		EventDispatcher.dispatchEvent(new DialogRoomsClearedEvent(this));
		EventDispatcher.dispatchEvent(new DialogEmployeesClearedEvent(this));
	}
	public void setNotice(Notice notice) {
		this.notice = notice;
	}
}
